﻿using System.Windows.Threading;

namespace DiskUsage
{
    /// <summary>
    /// http://bea.stollnitz.com/blog/?p=59
    /// How can I expand items in a TreeView? – Part III
    /// </summary>
    public static class DispatcherHelper
    {
        public static void WaitForPriority(DispatcherPriority priority)
        {
            DispatcherFrame frame = new DispatcherFrame();
            DispatcherOperation dispatcherOperation = Dispatcher.CurrentDispatcher.BeginInvoke(priority,
                new DispatcherOperationCallback(ExitFrameOperation), frame);
            Dispatcher.PushFrame(frame);
            if (dispatcherOperation.Status != DispatcherOperationStatus.Completed)
            {
                dispatcherOperation.Abort();
            }
        }

        private static object ExitFrameOperation(object obj)
        {
            ((DispatcherFrame)obj).Continue = false;
            return null;
        }
    }
}
